package edu.isquen.core.user.dao;

import java.util.List;

import edu.isquen.core.base.dao.BaseDAO;
import edu.isquen.core.base.dao.DataPackage;
import edu.isquen.core.base.model.ValueObject;
import edu.isquen.core.user.model.User;
import edu.isquen.util.CommUtil;

public class UserDAO extends BaseDAO<User> implements IUserDAO {
	
	public UserDAO(String className) {
		super(className);
	}

	public User login(String loginname, String loginpwd) throws Exception {
		User rtn = null;
		String hql = "FROM " + this.voClazzName + " WHERE loginno='" + loginname + "' and loginpwd='"+loginpwd+"'";
		List<User> result = (List<User>) this.getDatas(hql).datas;
		if (!result.isEmpty()) {
			rtn = (User) result.get(0);
		}
		return rtn;
	}
	
	public DataPackage<User> findByAny(ValueObject obj) throws Exception {
		User user = (User) obj;
		String dql = "FROM " + this.voClazzName + " where 1=1";
		if (CommUtil.validateString(user.getId())) {
			dql += " and id='" + user.getId() + "'";
		}
		if (CommUtil.validateString(user.getLoginno())) {
			dql += " and loginno='" + user.getLoginno() + "'";
		}
		if (CommUtil.validateString(user.getLoginpwd())) {
			dql += " and loginpwd='" + user.getLoginpwd() + "'";
		}
		return getDatas(dql);
	}
}
